package com.second.service.impl;


import com.second.consts.CouponStatus;
import com.second.mapper.IFCouponMapper;
import com.second.mapper.IFCouponUserMapper;
import com.second.model.dto.LAndR.LAndRResult;
import com.second.model.pojo.FCoupon;
import com.second.model.pojo.FCouponUser;
import com.second.service.IFCouponUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;



@Service
@Transactional
public class FCouponUserServiceImpl implements IFCouponUserService {


    @Autowired
    private IFCouponUserMapper couponuserDao;

    @Autowired
    private IFCouponMapper couponDao;

    /**
     * 展示待使用
     * @param userId
     * @return
     */
    @Override
    public List<FCoupon> findAllusercoupon(Integer userId) {
        //传入userId来查询用户优惠券表
        List<FCouponUser> CouponList = couponuserDao.findAllByUserId(userId);
//        List<FCouponUser> sendCouponList = new ArrayList<>();
        Date date = new Date();
        List<FCoupon> coupon1 = new ArrayList<>();
        if (CouponList != null && CouponList.size() > 0) {
            for (FCouponUser f : CouponList) {
                //由用户优惠券表的couponId进去优惠券表
                Optional<FCoupon> coupon = couponDao.findById(f.getCouponId());

                System.out.println(coupon.get());

                if (coupon.get().getEffectiveDays() != null) {
                    //在有效期内的 且领取未使用的
                    if (f.getStatus().equals(CouponStatus.Coupon_Using) && coupon.get().getEffectiveDays().getTime() > date.getTime() ){
//                        sendCouponList.add(CouponList.get(i));
                        coupon1.add(coupon.get());
                    }
                }

            }

        }
        return coupon1;
    }

    /**
     * 领取
     * 根据条件将用户优惠券表的userid和优惠券表的id写入
     * @param couponId
     * @param userId
     * @return
     */
    @Override
    public LAndRResult receiveCoupon(Integer couponId, Integer userId) {

        FCoupon sendCoupon = couponDao.getById(couponId);
        couponDao.updateStatus(couponId);
        FCouponUser ownCoupon = new FCouponUser();
        ownCoupon.setCouponId(couponId);
        ownCoupon.setUserId(userId);
        Date date = new Date();
        //如果在有效时间内并且状态是未使用那么写入一个创建日期
        if (sendCoupon.getEffectiveDays().getTime() > date.getTime()) {
            //时间格式转化
            java.sql.Timestamp sql_date = new java.sql.Timestamp(date.getTime());
            ownCoupon.setCreateDate(sql_date);
        }
        //将领取状态变为1已领取
        ownCoupon.setStatus(CouponStatus.Coupon_Using);
        FCouponUser rows = couponuserDao.save(ownCoupon);
        if (rows != null) {
            return new LAndRResult("200","领取成功",ownCoupon);
        }
        return new LAndRResult("500","领取失败");
    }


    /**
     * 过期优惠券
     * @param userId
     * @return
     */
    @Override
    public  List<FCoupon> usedByuserId(Integer userId) {
        List<FCouponUser> ownCoupon = couponuserDao.findAllByUserId(userId);
        List<FCoupon> coupon1 = new ArrayList<>();
        Date date = new Date();
        if (ownCoupon != null && ownCoupon.size() > 0) {
            for (int i = 0; i < ownCoupon.size(); i++) {
                Optional<FCoupon> coupon = couponDao.findById(ownCoupon.get(i).getCouponId());
                if (coupon.get().getEffectiveDays().getTime() < date.getTime() && ownCoupon.get(i).getStatus().equals(CouponStatus.Coupon_Using)) {
                    ownCoupon.get(i).setStatus(CouponStatus.Coupon_OutDate);//失效
                    couponuserDao.save(ownCoupon.get(i));

                }
            }
            for (int i = 0; i < ownCoupon.size(); i++) {
                Optional<FCoupon> coupon = couponDao.findById(ownCoupon.get(i).getCouponId());
                if (ownCoupon.get(i).getStatus().equals(CouponStatus.Coupon_OutDate)) {
                   coupon1.add(coupon.get());
                }
            }
        }

        return coupon1;
    }

    //使用优惠券
    @Override
    public int changeCouponStatusUsed(Integer couponId) {
        List<FCouponUser> couponUserList = couponuserDao.findAllByCouponIdAndStatus(couponId,CouponStatus.Coupon_Using);
        couponUserList.get(0).setStatus(CouponStatus.Coupon_Used);
        return 1;
    }

    /**
     *使用操作
     * @param Id
     * @return
     */
//    @Override
//    public LAndRResult updateById(Integer Id) {
//        FCouponUser ownCoupon = couponuserDao.getById(Id);
//        ownCoupon.setStatus(CouponStatus.Coupon_Used);
//        couponuserDao.save(ownCoupon);
//        return new LAndRResult("200","成功");
//    }

    /**
     * 展示已使用
     * @return
     */
    @Override
    public List<FCoupon> usingCoupon(Integer userId) {
        List<FCouponUser> ownCoupon = couponuserDao.findAllByUserId(userId);
        List<FCoupon> coupon1 = new ArrayList<>();
        for (FCouponUser f : ownCoupon) {
            if(f.getStatus().equals(CouponStatus.Coupon_Used)){
               //由用户优惠券表的couponId进去优惠券表
               Optional<FCoupon> coupon = couponDao.findById(f.getCouponId());
               //已使用的
               coupon1.add(coupon.get());
            }

        }

        return coupon1;
    }

}
